package com.sysmind.ravi.margin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sysmind.ravi.margin.dao.CommonDao;
import com.sysmind.ravi.margin.utils.JDBCHelper;

public class CommonDaoImpl implements CommonDao
{	Connection conn = null;
	PreparedStatement pstmt = null;
	Statement stmt = null;
	JDBCHelper jhelp = null;
	ResultSet rs = null;

	@Override
	public boolean deleteAllRows(String tableName) 
	{	jhelp = new JDBCHelper();
		conn = jhelp.getConnection();
		
		try
		{
			conn.setAutoCommit(false);
			String sql = "delete from "+tableName; 
			//String sql = "truncate table "+tableName;
			//System.out.println(sql);
			//pstmt = conn.prepareStatement(sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			conn.commit();
			return true;
		}
		catch(SQLException se)
		{	
			se.printStackTrace();
			return false;
		}
		finally
		{
			try
			{
				stmt.close();
				jhelp.closeConnection();	
			}
			catch(Exception se)
			{
				se.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		//return false;
	}

	@Override
	public int getRowCount(String tableName)
	{	jhelp = new JDBCHelper();
		conn = jhelp.getConnection();
	
		try
		{	conn.setAutoCommit(false);
			String sql = "SELECT COUNT(DISTINCT stock_id) AS rowcount FROM "+tableName;
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			int rowCount = -1;
			while(rs.next())
			{
				rowCount = rs.getInt("rowcount");
			}
			//System.out.println("RowCount : "+rowCount);
			return rowCount;
		}
		catch(SQLException se)
		{	
			se.printStackTrace();
			return -1;
		}
		finally
		{
			try
			{
				stmt.close();
				jhelp.closeConnection();	
			}
			catch(Exception se)
			{
				se.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		//return 0;
	}
	
	

}
